package application.services;

import application.exceptions.ItemNotFoundException;
import application.model.StandardsModel;
import application.model.modify.StandardModel;
import application.translators.Translator;
import application.translators.impl.StandardsTranslator;
import dao.StandardsDao;
import mappings.Standards;
import org.hibernate.exception.ConstraintViolationException;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("standards")
public class StandardsService
{
    private StandardsDao standardsDao = new StandardsDao();
    private Translator<List<Standards>, StandardsModel> translator = new StandardsTranslator();

    @Path("getAllStandards")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public StandardsModel getAllStandards()
    {
        return translator.translate(standardsDao.getAllStandards());
    }

    @Path("addStandard")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public StandardModel addNewStandard(@FormParam("name") String name)
    {
        StandardModel standardModel = new StandardModel();

        try
        {
            Standards standard = standardsDao.addStandards(name);
            standardModel.setId(standard.getId());
            standardModel.setName(standard.getName());
            standardModel.setStatus(0);
        }
        catch (ConstraintViolationException e)
        {
            // lapiemy wyjatek rzucany przez hibernate'a dla unique constraint - ustawiamy status 1 (rekord istnieje)
            standardModel.setStatus(1);
        }
        catch (Exception e)
        {
            // lapiemy wszystkie inne exceptiony i ustawiamy status na 2 (czyli cos sie wyjebalo, ale nie wiadomo co :P)
            standardModel.setStatus(2);
        }

        return standardModel;
    }

    @Path("editStandard")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public StandardModel editStandard(@FormParam("id") Integer id, @FormParam("name") String name)
    {
        StandardModel standardModel = new StandardModel();

        try
        {
            Standards standard = standardsDao.editStandard(id, new Standards(name));
            standardModel.setName(standard.getName());
            standardModel.setId(standard.getId());
            standardModel.setStatus(0);
        }
        catch (ItemNotFoundException e)
        {
            standardModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            standardModel.setStatus(2);
        }

        return standardModel;
    }

    @Path("deleteStandard")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public StandardModel deleteStandard(@FormParam("id") Integer id)
    {
        StandardModel standardModel = new StandardModel();

        try
        {
            standardsDao.deleteStandard(id);
            standardModel.setStatus(0);
        }
        catch (ItemNotFoundException e)
        {
            standardModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            standardModel.setStatus(2);
        }

        return standardModel;
    }
}
